﻿using Ivory;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using DefCore.AI;
using DefCore.Core;
using DefCore.Tools;
using DefCore.Mapping;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Krypton.Lights;
using Krypton;
using DefCore.Interfaces;
using DefCore.AI.Pathing;
using Microsoft.Xna.Framework.Input;

namespace DefCore.Structures
{
    public abstract class Blueprint : IViewable
    {
        //Fields
        public BlueprintFrameType FrameType { get; private set; }
        public BlueprintFrameAllowance AllowedFrameLocations { get; private set; }
        public Vector2 Size { get; private set; }

        private Rectangle? _HitRect;
        public Rectangle? HitRect 
        {
            get
            {
                if (_HitRect == null)
                    return new Rectangle(
                        (int)(Mouse.GetState().X - (Size.X / 2)),
                        (int)(Mouse.GetState().Y - (Size.Y / 2)),
                        (int)Size.X,(int) Size.Y);

                return (Rectangle)_HitRect;
            }
            set { _HitRect = (Rectangle?)value; }
        }
        
        //ctor
        public Blueprint(Vector2 size, BlueprintFrameType type, BlueprintFrameAllowance allowedFrames = 0)
        {
            this.Size = size;
            this.FrameType = type;

            if (allowedFrames != 0)
                this.AllowedFrameLocations = allowedFrames;
            else
            {
                if (type == BlueprintFrameType.FULLFRAME)
                    allowedFrames = BlueprintFrameAllowance.FULLFRAME;
                else
                    allowedFrames = BlueprintFrameAllowance.ALLSEMIFRAME;
            }

            AllowedFrameLocations = allowedFrames;
        }

        //Methods        
        public abstract Structure CreateStructure(CellBase cell, StructureFrameEnum frame);

        protected abstract void RenderBlueprint(SpriteBatch batch, GameTime time);

        public virtual bool ValidatePlacement(CellBase cell) { return true; }
        public virtual bool Load() { return true; }
        protected virtual void SolveBlueprint(InputControl control, GameTime time) { }        
                
        public void Render(SpriteBatch batch, GameTime time)
        {
            RenderBlueprint(batch, time);
        }
        public void Solve(InputControl control, GameTime time)
        {
            HitRect = new Rectangle(
                (int)((control.MouseVect.X + Objects.GameCamera.CameraTopLeft.X) - Size.X / 2),
                (int)((control.MouseVect.Y + Objects.GameCamera.CameraTopLeft.Y) - Size.Y / 2),
                (int)Size.X, (int)Size.Y);

            SolveBlueprint(control, time);
        }
    }
}
